﻿/*Copyright (C) 2010-2012 RAAFCOM B.V. All rights reserved.

This file is part of Velsto and may be used under the terms of:
The GNU General Public License version 3 or higher, as published by
the Free Software Foundation.

This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.*/
/*
* Application  : Velsto
* Class        : LayerparamsDataTable
* 04/06/2010-  : M.A.van Ravesteijn (Raafcom)
*   02/09/2010   Initial version v1.0
* 
* Class to maintain Layer Parameters DataTable as cache for Layer Parameters
* Data Grid View Dialogs and use for Simulation
*/ 
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;

namespace Velsto
{
    public class LayerparamsDataTable : DataTable
    {
        //Setup Data Table
        public LayerparamsDataTable()
        {
            DataColumn c1 = new DataColumn();
            c1.DataType = System.Type.GetType("System.Int32");
            c1.ColumnName = "LayerNr";
            c1.ReadOnly = false;
            this.Columns.Add(c1);
            DataColumn c2 = new DataColumn();
            c2.DataType = System.Type.GetType("System.String");
            c2.ColumnName = "GridTWTfile";
            c2.ReadOnly = false;
            this.Columns.Add(c2);
            DataColumn c3 = new DataColumn();
            c3.DataType = System.Type.GetType("System.String");
            c3.ColumnName = "TwtDev";
            c3.ReadOnly = false;
            this.Columns.Add(c3);
            DataColumn c4 = new DataColumn();
            c4.DataType = System.Type.GetType("System.String");
            c4.ColumnName = "GridTwtDevFile";
            c4.ReadOnly = false;
            this.Columns.Add(c4);
            DataColumn c5 = new DataColumn();
            c5.DataType = System.Type.GetType("System.String");
            c5.ColumnName = "K";
            c5.ReadOnly = false;
            this.Columns.Add(c5);
            DataColumn c6 = new DataColumn();
            c6.DataType = System.Type.GetType("System.Int32");
            c6.ColumnName = "V0vint";
            c6.ReadOnly = false;
            this.Columns.Add(c6);
            DataColumn c7 = new DataColumn();
            c7.DataType = System.Type.GetType("System.String");
            c7.ColumnName = "GridVFile";
            c7.ReadOnly = false;
            this.Columns.Add(c7);
            DataColumn c8 = new DataColumn(); 
            c8.DataType = System.Type.GetType("System.Int32");
            c8.ColumnName = "V0vintDev";
            c8.ReadOnly = false;
            this.Columns.Add(c8);
            DataColumn c9 = new DataColumn();
            c9.DataType = System.Type.GetType("System.String");
            c9.ColumnName = "GridVDevFile";
            c9.ReadOnly = false;
            this.Columns.Add(c9);
            DataColumn c10 = new DataColumn();
            c10.DataType = System.Type.GetType("System.Int32");
            c10.ColumnName = "DepVel";
            c10.ReadOnly = false;
            this.Columns.Add(c10);
            DataColumn c11 = new DataColumn();
            c11.DataType = System.Type.GetType("System.Int32");
            c11.ColumnName = "DepV0k";
            c11.ReadOnly = false;
            this.Columns.Add(c11);
            DataColumn c12 = new DataColumn();
            c11.DataType = System.Type.GetType("System.String");
            c11.ColumnName = "Layername";
            c11.ReadOnly = false;
            this.Columns.Add(c12);
        }
        // Method to parse with both decimal separators
        public double parseDoubleString(string sd)
        {
            var currentCulture = System.Globalization.CultureInfo.InstalledUICulture;
            var numberFormat = (System.Globalization.NumberFormatInfo)
            currentCulture.NumberFormat.Clone();
            numberFormat.NumberDecimalSeparator = ".";
            double number;
            bool succeeded = double.TryParse(sd, System.Globalization.NumberStyles.Any, numberFormat, out number);
            if (succeeded)
            {
                return number;
            }
            else
            {
                numberFormat.NumberDecimalSeparator = ",";
                bool succeeded2 = double.TryParse(sd, System.Globalization.NumberStyles.Any, numberFormat, out number);
                if (succeeded2)
                {
                    return number;
                }
                else
                {
                    return -999.9;
                }
            }
        }
        //Fill Data Table from Simulation Entity
        public void fillLayerparamsDataTable(SimulationEntity se)
        {
            DataRow row;
            for (int i = 0; i < se.NrLayers; i++)
            {
                row = this.NewRow();
                row[0] = i + 1;
                row[1] = (String)se.AlInputFileTWT[i];
                row[2] = ((LayerparamsEntity)(se.AlLayerParams[i])).TwtDev.ToString();
                row[3] = (String)se.AlInputFileTWTdev[i];
                row[4] = ((LayerparamsEntity)(se.AlLayerParams[i])).K.ToString();
                row[5] = ((LayerparamsEntity)(se.AlLayerParams[i])).V0vint;
                row[6] = (String)se.AlInputFileV0[i];
                row[7] = ((LayerparamsEntity)(se.AlLayerParams[i])).V0vintDev;
                row[8] = (String)se.AlInputFileVVNdev[i];
                row[9] = ((LayerparamsEntity)(se.AlLayerParams[i])).DepVel;
                row[10] = ((LayerparamsEntity)(se.AlLayerParams[i])).DepV0K;
                row[11] = (String)se.AlLayername[i];
                this.Rows.Add(row);
            }
        }

        //update Simulation Entity cache after updates in Data Grid View/Data Table
        public SimulationEntity updateSimulationEntity(SimulationEntity se)
        {
            // Fill Layersparameters copy V0vintOption from input SimulationEntity
            ArrayList alLP = new ArrayList();
            for (int i = 0; i < se.NrLayers; i++)
            {
                LayerparamsEntity lp = new LayerparamsEntity();
                //((DataGridViewComboBoxCell)(lpDgv.Rows[i].Cells[Constants.CELL_COL_VSELCOMBO])).Value =
                //            alVtypes[((LayerparamsEntity)se.AlLayerParams[i]).V0vintOption];
                lp.V0vintOption = ((LayerparamsEntity)se.AlLayerParams[i]).V0vintOption;
                lp.V0vint = (Int32) Rows[i][5];
                lp.V0vintDev = (Int32) Rows[i][7];
                string s1 = ((String)Rows[i][4]).Replace(',', '.');
                lp.K = parseDoubleString(s1);
                lp.KDev = 0.0;
                lp.F = 1.0;
                lp.DepV0K = 0;
                s1 = ((String)Rows[i][2]).Replace(',', '.');
                lp.TwtDev = parseDoubleString(s1);
                lp.DepVel = 0;
                alLP.Add(lp);
            }
            for (int i = se.NrLayers; i < 10; i++)
            {
                LayerparamsEntity lp = new LayerparamsEntity();
                alLP.Add(lp);
            }

            ArrayList alTWT = new ArrayList();
            ArrayList alV = new ArrayList();
            ArrayList alVVNdev = new ArrayList();
            ArrayList alTWTdev = new ArrayList();
            ArrayList alLayername = new ArrayList();
            // Read Gridfiles catch empty Cells
            for (int i = 0; i < se.NrLayers; i++)
            {
                // catch case DataGridView Cell is emptied by user (generates DBNull)
                try
                {
                    string as1 = (string) Rows[i][Constants.LPDT_COL_TWTFILE];
                    alTWT.Add(as1);
                }
                catch (InvalidCastException ex)
                {
                    alTWT.Add("");
                }
                
                try
                {
                    string as1 = (string) Rows[i][Constants.LPDT_COL_VFILE];
                    alV.Add(as1);
                }
                catch (InvalidCastException ex)
                {
                    alV.Add("");
                }
                
                try
                {
                    string as1 = (string) Rows[i][Constants.LPDT_COL_VDEVFILE];
                    alVVNdev.Add(as1);
                }
                catch (InvalidCastException ex)
                {
                    alVVNdev.Add("");
                }
   
                try
                {
                    string as1 = (string) Rows[i][Constants.LPDT_COL_TWTDEVFILE];
                    alTWTdev.Add(as1);
                }
                catch (InvalidCastException ex)
                {
                    alTWTdev.Add("");
                }
                
                try
                {
                    string as1 = (string)Rows[i][Constants.LPDT_COL_LAYERNAME];
                    alLayername.Add(as1);
                }
                catch (InvalidCastException ex)
                {
                    alLayername.Add("");
                }
            }
            for (int i = se.NrLayers; i < 10; i++)
            {
                alTWT.Add("");
                alV.Add("");
                alVVNdev.Add("");
                alTWTdev.Add("");
                alLayername.Add("");
            }
 
            se.AlLayerParams = alLP;
            se.AlInputFileTWT = alTWT;
            se.AlInputFileV0 = alV;
            se.AlInputFileVVNdev = alVVNdev;
            se.AlInputFileTWTdev = alTWTdev;
            se.AlLayername = alLayername;
            return se;
        }

        // Insert new row.
        public void insertRow(int insertatindex)
        {
            LayerparamsEntity lp = new LayerparamsEntity();
            DataRow row;
            /*this.Columns["LayerNr"].ReadOnly = false;*/
            row = this.NewRow();
            row[0] = insertatindex;
            row[5] = lp.V0vint;
            row[7] = lp.V0vintDev;
            row[4] = lp.K;
            row[2] = lp.TwtDev;
            row[1] = "";
            row[3] = "";
            row[6] = "";
            row[8] = "";
            row[9] = lp.DepVel;
            row[10] = lp.DepV0K;
            row[11] = "";
            this.Rows.InsertAt(row, insertatindex);
            
            for (int i = 0; i < this.Rows.Count; i++)
            {
                this.Rows[i][0] = i + 1;
            }
        }

    }
}
